package com.chh.controller;

import com.baidu.bjf.remoting.protobuf.Any;
import com.baidu.bjf.remoting.protobuf.Codec;
import com.baidu.bjf.remoting.protobuf.ProtobufProxy;
import com.chh.jproto.HouseJProto;
import com.chh.jproto.PeopleJProto;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 一碗情深
 * @since 2024-08-09 星期五 14:03:44
 */
@RestController
@RequestMapping("/house")
public class HouseController {

    @RequestMapping(value = "/index", consumes = MediaType.APPLICATION_PROTOBUF_VALUE, produces = MediaType.APPLICATION_PROTOBUF_VALUE)
    public byte[] index(@RequestBody byte[] houseJProto) throws IOException {
        Codec<HouseJProto> simpleTypeCodec = ProtobufProxy.create(HouseJProto.class);
        System.out.println(simpleTypeCodec.decode(houseJProto).toString());
        // 时间
        long time = System.currentTimeMillis();
        // 获取用户信息
        PeopleJProto owner = new PeopleJProto("4212434335", "小明", true, 40000.0);
        // 合伙人信息
        Any ownerExtent = Any.pack(new PeopleJProto("534243242", "小李", false, 80000.0));
        // house信息
        List<PeopleJProto> owners = new ArrayList<>();
        owners.add(owner);
        return simpleTypeCodec.encode(new HouseJProto(1, HouseJProto.HouseType.OTHERS, (int) time, 2000000L, owners, ownerExtent));
    }

}
